import { ref, onMounted } from "vue";
import { getTopCategoryApi } from "@/apis/category.js";
import { useRoute , onBeforeRouteUpdate} from "vue-router";


export  function useCategory(){

  const categoryDate = ref({});
  const route = useRoute();
  const getTopCategory = async (id = route.params.id) => {
    const res = await getTopCategoryApi(id);
    categoryDate.value = res.result;
  };
  onMounted(() => {
    getTopCategory();
  });
  // 监听路由发生变化
onBeforeRouteUpdate((to) => {
  console.log(to.params.id)
  getTopCategory(to.params.id)
})

  return {
    categoryDate

  }

}
